package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取当前用户的配送地址信息
     * @return
     */
    @GetMapping("list")
    @ApiOperation(value = "获取当前用户的配送地址信息")
    public Result<List<AddressBook>> list(){
        List<AddressBook> list =addressBookService.list();
        return Result.success(list);
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增地址")
    public Result save(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@RequestParam Long id){
        log.info("传入的id是：{}",id);
        AddressBook list = addressBookService.getById(id);
        return Result.success(list);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation(value = "根据id修改地址")
    public Result updateById(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "根据id删除地址")
    public Result delById(Long id){
        addressBookService.delById(id);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @return
     */
    @PutMapping("/default")
    @ApiOperation(value = "设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("传入的数据为：{}",addressBook);
        Long id = addressBook.getId();
        addressBookService.setDefault(id);
        return Result.success();
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }
}
